package com.adams.app.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.adams.app.AppData;
import com.adams.app.R;
import com.adams.app.common.Global;
import com.adams.app.listener.MyLocationListener;
import com.adams.app.listener.MySearchListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.platform.comapi.basestruct.GeoPoint;

/**
 * @description 百度地图
 * 
 * @author ChengBin
 * 
 */
public class MyMapActivity extends ActionBarActivity {

	private BMapManager mBMapMan = null;
	private MapView mMapView = null;
	private MKSearch mMKSearch = null;
	private MapController mMapController;
	private AppData appData;

	// 定位
	private LocationClient mLocationClient = null;

	// 路线选择
	private RadioGroup group;
	private RadioButton walkBtn;
	private MKPlanNode endNode = new MKPlanNode();
	MKPlanNode startNode = new MKPlanNode();
	MySearchListener listener;
	private String name;
	private PopupOverlay overlay;
	private View view;

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			// 终点位置
			if (msg.what == Global.STOP_POS) {
				GeoPoint endPoint = (GeoPoint) msg.obj;
				endNode.pt = endPoint;
			}

			// 开始位置
			if (msg.what == Global.START_POS) {
				BDLocation start = (BDLocation) msg.obj;
				GeoPoint startPoint = new GeoPoint(
						(int) (start.getLatitude() * 1E6),
						(int) (start.getLongitude() * 1E6));
				startNode.pt = startPoint;
			}

			if (mMKSearch != null && startNode.pt != null && endNode.pt != null
					&& Global.isFirst) {

				GeoPoint point2 = endNode.pt;
				showPop(point2);
				
				mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
				mMKSearch.walkingSearch(null, startNode, null, endNode);
				Global.isFirst = false;
			}
		}
	};

	public void showPop(GeoPoint point) {
		((TextView) view.findViewById(R.id.map_bubbleTitle)).setText(name);
		
		overlay.showPopup(view, point, 20);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		initView();
		initMapParams();

	}

	/**
	 * 
	 * @description 初始化控件
	 * 
	 * @author ChengBin
	 * @date 2013年12月21日下午2:49:39
	 */
	public void initView() {
		group = (RadioGroup) this.findViewById(R.id.rgSearchMode);
		walkBtn = (RadioButton) this.findViewById(R.id.rbWalking);
		walkBtn.setChecked(true);
		group.setOnCheckedChangeListener(groupCheckListener);
		view = LayoutInflater.from(MyMapActivity.this).inflate(
				R.layout.overlay_popup, null);
		view.setPadding(5, 0, 5, 0);
	}

	private String address;

	private void initMapParams() {
		// 设置MapView
		mMapView = (MapView) findViewById(R.id.mapsView);
		mMapView.setClickable(true);
		mMapView.setTraffic(true);

		// 设置mMapController
		appData = (AppData) getApplication();
		mBMapMan = appData.getMapManager();
		mBMapMan.start();
		mMapController = mMapView.getController(); // 设置启用内置的缩放控件
		mMapController.enableClick(true);
		mMapController.setZoom(13); // 设置地图zoom级别

		// 获取目的地
		AppData.flag = Global.CITY_NAME;
		name = getIntent().getExtras().getString(Global.NAME);
		address = getIntent().getExtras().getString(Global.ADDRESS);
		mMKSearch = appData.getMkSearch();
		mMKSearch.init(mBMapMan, new MySearchListener(this, mMapView, handler));
		mMKSearch.geocode(address, Global.CITY_NAME);
		overlay = new PopupOverlay(mMapView, null);
		
		// 获取mLocationClient，定位
		mLocationClient = appData.getLocationClient();
		mLocationClient.registerLocationListener(new MyLocationListener(
				handler, mMapView));
		mLocationClient.start();
		mLocationClient.requestLocation();
	}

	OnCheckedChangeListener groupCheckListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.rbDriving:
				Toast.makeText(MyMapActivity.this, "请稍等，正在为您切换驾车路线...",
						Toast.LENGTH_LONG).show();
				requestDriv(startNode, endNode);
				break;
			case R.id.rbWalking:
				Toast.makeText(MyMapActivity.this, "请稍等，正在为您切换步行路线...",
						Toast.LENGTH_LONG).show();
				requestWalk(startNode, endNode);
				break;
			case R.id.rbTransit:
				Toast.makeText(MyMapActivity.this, "请稍等，正在为您切换公交路线...",
						Toast.LENGTH_LONG).show();
				requestBus(startNode, endNode);
				break;
			default:

				break;
			}
		}
	};

	/**
	 * 
	 * @description 定位
	 * 
	 * @author ChengBin
	 * @date 2013年12月21日下午3:15:10
	 */
	public void requestLocation() {
		mLocationClient.start();
		if (mLocationClient != null) {
			if (!mLocationClient.isStarted()) {
				mLocationClient.start();
			}
			mLocationClient.requestLocation();
		} else {
			Log.d("LocSDK3", "locClient is null or not started");
		}
	}

	// 手动定位
	OnClickListener requestListener = new OnClickListener() {
		public void onClick(View v) {
			if (mLocationClient != null) {
				// MyLocationListener.isFirstLoc = true;
				Toast.makeText(MyMapActivity.this, "正在定位...",
						Toast.LENGTH_SHORT).show();
				requestLocation();
			}
		}
	};

	
	@Override
	protected void onPause() {
		if (mMapView != null) {
			mMapView.onPause();;
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mMapView != null) {
			mMapView.onResume();
		}
		super.onResume();
		Global.isFirst = true;
		Global.isOk = true;
	}

	@Override
	protected void onDestroy() {
		// 退出时销毁定位
		if (mLocationClient != null)
			mLocationClient.stop();
		if (mMapView != null) {
			mMapView.destroy();
			mMapView =  null;
		}
		if (mMKSearch != null) {
			mMKSearch.destory();
			mMKSearch = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mMapView.onRestoreInstanceState(savedInstanceState);
	}

	public void requestWalk(MKPlanNode start, MKPlanNode end) {
		if (start.pt != null && end.pt != null) {
			mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
			mMKSearch.walkingSearch(null, start, null, end);
		}
	}

	public void requestDriv(MKPlanNode start, MKPlanNode end) {
		if (start.pt != null && end.pt != null) {
			mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
			mMKSearch.drivingSearch(null, start, null, end);
		}
	}

	public void requestBus(MKPlanNode start, MKPlanNode end) {
		if (start.pt != null && end.pt != null) {
			mMKSearch.setTransitPolicy(MKSearch.ECAR_TIME_FIRST);
			mMKSearch.transitSearch(Global.CITY_NAME, start, end);
		}
	}

}
